﻿#include "commonpage.h"
#include "ui_commonpage.h"
#include "listitembox.h"
#include <QListWidgetItem>
#include <QDebug>
#include <QPushButton>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    //去除水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        emit playAll(pageType);});
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        emit playMusicByIndex(this,index.row());});
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUI(const QString &title, const QString &imagePath)
{
    ui->pageTittle->setText(title);

    ui->musicImageLabel->setPixmap(QPixmap(imagePath));

    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType=pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    musicListOfPage.clear();

    for(Music &e:musicList)
    {
        switch (pageType) {
        case LOCAL_PAGE:
            musicListOfPage.push_back(e.getMusicId());
            break;
        case LIKE_PAGE:
        {
            if(e.getIsLike())
            {
                musicListOfPage.push_back(e.getMusicId());
            }
            break;
        }
        case HISTORY_PAGE:
        {
            if(e.getIsHistory())
            {
                musicListOfPage.push_back(e.getMusicId());
            }
            break;
        }
        default:
            qDebug()<<"未知类型";
            break;
        }
    }
}

void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);

    for(auto musicId:musicListOfPage)
    {
        auto it=musicList.findMusicById(musicId);
        if(it==musicList.end()) continue;

        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeIcon(it->getIsLike());

        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
        ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);

        connect(listItemBox,&ListItemBox::setIsLike,this,[=](bool isLike){
            //更新歌曲状况
            //通知三个页面进行更新歌曲的信息
            emit updateLikeMusic(isLike,it->getMusicId());
        });
    }
    // 更新完成后刷新下界⾯
    repaint();
}

void CommonPage::addMusicToMediaplayList(MusicList &musicList, QMediaPlaylist *playList)
{
    for(Music& music:musicList)
    {
        switch (pageType) {
        case LIKE_PAGE:
            if(music.getIsLike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_PAGE:
            playList->addMedia(music.getMusicUrl());
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug()<<"未知";
            break;
        }
    }
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

const QString &CommonPage::getMusicIdByIndex(int index)
{
    if(index >= musicListOfPage.size())
    {
        qDebug()<<"无此歌曲";
    }
    return musicListOfPage[index];
}

